// SlagCar/Transit/Transit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,  //是否显示选择工地
    orderList: [],  //工地列表
    value: '', //选中的工地名称
    main: null,
    detail: null,

    destinationList: []//运输任务列表
  },

  //显示选择工地
  bindShow(){
    this.setData({show: true})
  },
  // 工地列表收起
  bindClose(){
    this.setData({show: false})
  },
  // 搜索
  bindSearch(e){
    let val = e.detail,
      {orderList} = this.data;
    console.log(orderList);
    orderList.forEach(item => {
      item.isHide = (item.reference1_all_delivery_value.indexOf(val) < 0 && (item.Order_client_ID_value && item.Order_client_ID_value.indexOf(val) < 0))
    });
    this.setData({orderList})
  },
  //工地选择
  bindSelect(e){
    let {item} = e.currentTarget.dataset;
    this.setData({main: item,show: false})
    this.getDestination(item.row_id);
  },

  //获取工地
  getOrderMain(){
    wx.showLoading({mask: true});
    let url = 'ZTAuth/muck_order_main_isused_driver';
    wx.$get(5,url).then(res =>{
      if (res.errcode != 0) return;
      let data = res.data.row || [],
        data0 = data[0],
        obj = {orderList: data};

      if (data0){
        obj.main = data0;
        this.getDestination(data0.row_id);
      }else {
        wx.hideLoading();
      }
      this.setData(obj);
    })
  },
  //获取任务
  getDestination(id){
    wx.showLoading({mask: true});
    let url = 'DynamicQuery/query_dynamic_v3',
      post = {
        "oby":"muck_order_assign_destination.row_id",
        "table_id":"muck_order_assign_destination",
        "page":1,"limit":0,
        "SearchWhere":{
          "filters": [
            {"CompareWith":"muck_order_assign_destination.parent_row_id","Operator":"=","CompareValue": id,"or":0},
            {"CompareWith":"muck_order_assign_destination.is_used","Operator":"=","CompareValue": true,"or":0},
          ],
          type: 'and'
      }
    };
    wx.$post(3,url,post).then(res => {
      if (res.errcode != 0) return;
      wx.hideLoading();
      let data = res.data.row || [];
      this.setData({destinationList: data})
    })
  },
  //选中任务
  bindSelectDestination(e){
    let {index} = e.currentTarget.dataset,
      {destinationList } = this.data;
    /*this.setData({
      [`destinationList[${index}].checked`]: !destinationList[index].checked
    })*/
    this.setData({
      detail: destinationList[index]
    })
  },

  //提交运输任务
  bindSubmit(){
    let {main,detail} = this.data,
      msg = '';
    if (!main) msg = '请选择工地';
    if (!detail) msg = '请选择消纳场';
    if (msg) {
      wx.$toast(msg)
      return
    }
    let post = {
      "order_row_id": main.row_id,
      "order_assign_destination_row_id": detail.row_id
    },
      url = 'ZTAuth/make_driver_waybill';
    wx.showLoading({mask: true});
    wx.$post(5,url,post,).then(res => {
      if (res.errcode != 0) {
        if (res.errmsg === '有未完成的运单不可再接新单') {
          this.getIncompleteOrder().then(res => {
            if (res){
              setTimeout(() =>{
                wx.navigateTo({
                  url: '../WaybillDetail/WaybillDetail?row_id=' + res.row_id
                })
              },1000)
            }
          });
        }
        return;
      }
      wx.$toast('提交成功');
      this.getIncompleteOrder().then(res => {
        if (res){
          setTimeout(() =>{
            wx.navigateTo({
              url: '../WaybillDetail/WaybillDetail?row_id=' + res.row_id
            })
          },1000)
        }
      });
    })

  },

  //获取司机未完成订单
  getIncompleteOrder(){
    return new Promise(resolve => {
      let url = 'ZTAuth/driver_waybill_incomplete';
      wx.$get(5,url).then(res => {
        if (res.errcode != 0) return;
        let data = res.data.row && res.data.row[0];
        resolve(data)
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getIncompleteOrder().then(data => {
      if (data){
        wx.showModal({
          title: '运单进行中',
          content: '当前有运单进行中，跳转到运单详情',
          confirmText: '跳转',
          success: (res) => {
            if(res.confirm) {
              wx.navigateTo({
                url: '../WaybillDetail/WaybillDetail?row_id=' + data.row_id
              })
            }
          },
        })
      }
    })
    this.getOrderMain();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
